package zucc.edu.repay.controller;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import Bean.user;
import Dao.*;

import Util.BaseException;
@Controller
public class UserController {
	@RequestMapping(value = "/test.do") 
	@ResponseBody
	public String login(HttpServletRequest request, @RequestBody String params) throws JSONException, BaseException{  	
	
		JSONObject json = new JSONObject(params);

    	String userId = (String) json.get("userId");
    	String pwd = (String) json.get("pwd");
    	JSONObject jo = new JSONObject();
    	user user =new UserDao().searchUser(userId);
    	
    	if(user != null && pwd.equals(user.getPwd())){
    		if(user.getUserPermissions().equals("user")){
        		request.getSession().setAttribute("userid", user.getUserId());
        		jo.put("msg", "succ");
        	}
        	else{
        		request.getSession().setAttribute("userid", user.getUserId());
        		jo.put("msg", "asucc");
        	}
    	}
    	else 
    		jo.put("msg", "fail");
			
			        
		return jo.toString(); 
    }    
	
	
	@RequestMapping(value = "delete",method=RequestMethod.POST) 
	@ResponseBody
	public String deleteuser(@RequestBody String params) throws JSONException, BaseException{
		JSONObject json = new JSONObject(params);
    	String userId = (String) json.get("userId");
    	JSONObject jo = new JSONObject();
    	user user =	new UserDao().searchUser(userId);
    	new UserDao().delUser(user);
    	jo.put("msg", "succ");
    	return jo.toString();
	}
	
	@RequestMapping(value = "cancel",method=RequestMethod.POST) 
	@ResponseBody
	public String canel(HttpServletRequest request, @RequestBody String params) throws JSONException{  	
    	JSONObject json = new JSONObject(params);
    	String userId = (String) json.get("userId");
	    request.getSession().removeAttribute("userid");	   
	   JSONObject jo = new JSONObject();
	   jo.put("msg", "succ");
		return jo.toString(); 
    }
	
	@RequestMapping(value = "adduser",method=RequestMethod.POST) 
	@ResponseBody
	public String adduser(@RequestBody String params) throws JSONException, BaseException{
		JSONObject json = new JSONObject(params);
    	String userId = (String) json.get("userId");
    	String userName = (String) json.get("userName");
    	String userJob =(String)  json.get("userJob");
    	String userPermissions =(String)  json.get("userPermissions");
    	String cardNumber =(String)  json.get("cardNumber");
    	
    	JSONObject jo = new JSONObject();
    	
    	user user =	new UserDao().searchUser(userId);	
    	if(user!=null)
    		jo.put("msg", "error");
    	else{
    		user u= new user();
    		u.setUserId(userId);
    		u.setUserName(userName);
    		u.setUserJob(userJob);
    		u.setUserPermissions(userPermissions);
    		u.setCardNumber(cardNumber);
    		u.setPwd("123");
    	new UserDao().addUser(u);
    	jo.put("msg", "succ");
    	}
    	
			
    	return jo.toString(); 
	}
	
	@RequestMapping(value = "changepwd",method=RequestMethod.POST) 
	@ResponseBody
	public String updatepwd(@RequestBody String params) throws JSONException, BaseException{
		JSONObject json = new JSONObject(params);
		
    	String opwd = (String) json.get("opwd");
    	String npwd =(String)  json.get("npwd");
    	String userId = (String) json.get("userId");
    	JSONObject jo = new JSONObject();
    	
    	user user =	new UserDao().searchUser(userId);
    	if(!opwd.equals(user.getPwd())){
    		jo.put("msg", "error");   		
    	}
    	
    	else{
    		user.setPwd(npwd);
    		new UserDao().updateUser(user);
    		jo.put("msg", "succ");
    	}
    	
    	return jo.toString();
	}
}
